﻿var  SearchType = {
    None : "None",
    Text : "Text",
    Tag : "Tag",
    User : "User"
}
var  SearchTypeText = {
    Text : "Type in words separted by commas",
    Tag : "Type in tag(s) separated by commas",
    User : "Type in extact username"
}

var _textChanged = false;

function submitForm(formName) {
    document.forms[formName].submit();
}
function processEnter(e, textBoxId, formName)
{
    if(!e) e = window.event;
        	
    if (e.keyCode)
    {
        if (e.keyCode == 13 || e.which == 13)
        { 
            // if the textbox is blank then dont do the post back.
            if (jQuery.trim($("#" + textBoxId).val()) != '') {
                submitForm(formName);
            }
        }
    } 
}


this.markDirty = function()
{
    _textChanged = true;
}

this.isDirty = function() 
{
    return _textChanged;
}

function setupWatermark(targetElement, text) 
{
    if (jQuery.trim(targetElement.val()) == '')
    {
        targetElement.val(text);
        targetElement.addClass("waterMarked");
    }

    targetElement.blur(function() {
        if (jQuery.trim($(this).val()) == '') {
            $(this).addClass("waterMarked");
        }
        if (jQuery.trim(targetElement.val()) == '')
            targetElement.val(text);
    });

    targetElement.focus(function() {
        $(this).removeClass();
        if (jQuery.trim(targetElement.val()) == text)
            targetElement.val('');
    });
}


this.changeSearchType = function(type) {

     $("#type").val(type);

    var searchText = '';
    var textBox = $('#q');

    if (type == SearchType.Text) {
        searchText = SearchTypeText.Text;
    }
    else if (type == SearchType.Tag) {
        searchText = SearchTypeText.Tag;
    }
    else {
        searchText = SearchTypeText.User;
    }

    if (type != SearchType.None) {
    
        $(SearchType.Tag).removeClass();
        $(SearchType.Text).removeClass();
        $(SearchType.User).removeClass();

        $(type).addClass('selectedOption');
        
        if (_textChanged == false) {
            textBox.val("");
            setupWatermark(textBox, searchText);
        }
    }
    $('#criteria').hide();
}


this.togglePanel = function(elementId) 
{
    $("#" + elementId).slideToggle("normal");
}
       
function ignoreEnter(e)
{
    if(!e) e = window.event;

    var ctr = $('searchBox');
    
    if (e.keyCode == 13 || e.which == 13)
    {
        if( e.target == ctr )
        {
            return false;
        }
    }

    return true;
}

function getPhotoIdFromHash() {
    return window.location.hash.replace("#", '');
}

